<?php
namespace app\notice\service;

use app\common\library\Service;
use think\Db;

class Notice extends Service
{
    use \app\common\library\traits\Model;
    // 模型验证
    protected $modelValidate = null;
    // 批量验证
    protected $batchValidate = null;

    protected function _initialize() {
        parent::_initialize();
        $this->model = model('notice/Notice');
    }

    /**
     * 列表
     * @param mixed $maps   查询条件
     * @return mixed
     */
    public function lists($maps = '', $order = 'create_time DESC', $page = 0, $limit = 12, $field = true, $relations = [], $attrs =[]) {

        $result = $this->model->order($order)->field($field);

        if (!empty($maps['keywords'])) {
            $result = $result->where($maps['type'],'like','%'.$maps['keywords'].'%');
        }

        if($page !== false) {
            $result = $result->paginate($limit, '', ['page' => $page]);
        } else {
            $result = $result->limit($limit)->select();
        }

        /* 关联数据获取 */
        if(!empty($relations)) {
            foreach ($result as $key => $value) {
                array_map(function($e) use (&$value) {
                    $e = trim($e);
                    $value->$e = $value->$e ?: new \stdClass(); // 注意没有数据不要返回默认的 NULL
                }, $relations);
            }
        }

        /* 获取器数据 */
        if(!empty($attrs)) {
            foreach ($result as $key => $value) {
                array_map(function($attr) use (&$value) {
                    $attr = trim($attr);
                    return $value->$attr = $value->getAttr($attr);
                }, $attrs);
            }
        }
        return $result;
    }

    /**
     * 详情
     * @param [array]   $maps       [查询条件]
     * @param [string]  $field      [查询字段]
     * @param [array]   $relations  [关联数据]
     * @param [array]   $attrs      [获取器数据]
     * return mix
     */
    public function detail($id = ''){
        try {

            $info = $this->model->where('id',$id)->find();

            if (!$info) {
                $this->error = '通告信息未找到';
                return false;
            }
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }
        return $info;
    }

    /**
     * 添加
     * @param array $params 文章相关信息
     * @return mixed
     */
    public function create($params = []) 
    {
        try{
            $params['id'] = \fast\Uuid::create();
            $this->model->save($params);
        } catch (\Exception $e) {
            \Db::rollback();
            $this->error = $e->getMessage();
            return false;
        }
        return $this->model;
    }

    /**
     * 编辑
     * @param int $id   主键id
     * @param array $params 文章相关信息
     * @return mixed
     */
    public function edit($id = '', $params = []) 
    {
        try {
            $info = $this->model->where('id','=',$id)->find();
            if (!$info) {
                $this->error = '通告信息未找到';
                return false;
            }
            $params['id'] = $id;
            $info->save($params);
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }
        return $info;
    }

    /**
     * 软删除
     * @param int $id 文章id
     * @return mixed
     */
    public function del($id = 0) 
    {
        try {
            if (empty($id)) {
                $this->error = '要删除的公告信息不能为空';
                return false;
            }
            $info = $this->model->where('id','=',$id)->find();
            if (!$info) {
                $this->error = '公告信息未找到';
                return false;
            }
            $info->destroy(['id' => $id]);
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }
        return $info;
    }
}